$('.right-titleicon').on("click", function(e) {
	projectVM.showMenulist()
}) 
projectVM = new Vue({
	el: "#content",
	data: { 
		stationList:[],
		approveDesc: '',
		approveDialogTips: true,
		isApprove: false,
		isMyEditor: false,
		workStatus: '20',
		dealingPerson: 'P129951',
		//dealingPerson: 'P129952',
		// 是否是编辑
		isEdit: false,
		// 编辑对应行
		index: 0,
		approveCodeName: '',
		workType: '0402',
		// 流程id
		workId: "", // 获取流程日志所需待办Id
		addOrEdit: false,
		isEditor: false,
		initNetSafeDeviceInfoList: [],
		onStatus: false,
		showPop: false,
		showMenu: false,
		showAdd: false,
		elseDesc: '',
		//删除数据
		delNetSafeDeviceInfoList: [],
		defectInfoList: [],
		list: [],
		currentUser: {},
		// 基本信息
		baseForm: {},
		baseRules: {
			checkStaff: [],
			checkDate: []
		},
		// 光伏电场列表
		pTableLoading: false, // 列表Loading
		photFarmForm: {
			list: []
		},
		deleteList: [], // 删除的光伏电场列表
		photFarmRules: {
			stationName: [{
				required: true,
				message: '请输入光伏电场',
				trigger: 'blur'
			}]
		},
		// 巡检信息
		checkGroup: [{
				code: '10',
				name: '逆变室',
				listName: 'nbList'
			},
			{
				code: '11',
				name: '箱变',
				listName: 'xbList'
			},
			{
				code: '12',
				name: '汇流箱',
				listName: 'hlList'
			}
		],
		cTableLoading: false, // 列表Loading
		detailList: [],
		checkForm: {
			nbList: [], // 逆变室
			xbList: [], // 箱变
			hlList: [] // 汇流箱
		},
		// 数据字典
		startOrStop: [], // 启动停止
		normalOrNot: [], // 正常异常
		myUid: '',
		isEditAdd: false,
		// 数据字典
	},
	components: {
		personselect,
		processlog
	},
	created() {
		this.workId = JSON.parse(localStorage.getItem('todoWork')).obj.workId
		this.workStatus = JSON.parse(localStorage.getItem('todoWork')).obj.workStatus
		this.currentUser = JSON.parse(localStorage.getItem('neiosUser'))
		
		this.getStationCode()
		if (this.workStatus == '20') {
			this.isMyEditor = true

		}
		let dictCodes = "START_OR_STOP,NORMAL_OR_NOT";
		let dict = mui.neiosCommon.queryDict(dictCodes, (data) => {
			if (data != '') {
				this.startOrStop = data.START_OR_STOP;
				this.normalOrNot = data.NORMAL_OR_NOT;

			}
		})
		this.getPhotFarmCheckData()
	},
	beforeDestroy() {},
	methods: {
		//选择场站
			showStationList(index){
				var that = this
				 let picker = new mui.PopPicker({
					 layer:1
				 })
				 picker.setData(this.stationList)
				  picker.show(function(e) {   //e 就是总数据的数组 显示picker
					that.photFarmForm.list[index].stationCode = e[0].value
					that.photFarmForm.list[index].stationName = e[0].text
				})
			
			},
		//获取场站列表
		getStationCode(){
			var that = this
			var param = {
				orgType:'2',
				parentOrgCode:this.currentUser.companyNewCode,
				parentOrgType:'1'	
			}							 
			mui.neiosRequst.httpRequst('/common/organization/getOrganizationListByType', param,
				(data)=> {
				if(data.code ==  0){	
					let arr = []
					data.data.forEach(item=>{
						if(item.newOrgName != '' && item.newOrgName != null){
							arr.push({
								text:item.newOrgName,
								value:item.newOrgCode
							})
						}
						
					})
					that.stationList = arr
					
				}
				else{
					mui.toast(data.msg)
				}
			
				},
				function(data) {
				console.log(JSON.stringify(param))
				})
		},
		changeCheck(){},
		// 获取光伏电场巡检数据
		getPhotFarmCheckData() {
			let _this = this;
			let param = {
				workId: this.workId,
				newAreaCode: this.currentUser.companyNewCode
			};
			mui.neiosRequst.httpRequst('/app/photFarmCheck/getPhotFarmCheckData', param,
				(data) => {
					if (data.baseInfo == null) {
						_this.baseForm = {
							checkId: mui.neiosCommon.getUuidv4(),
							workId: _this.workId,
							newAreaCode: _this.currentUser.companyNewCode,
							checkCode: _this.currentUser.userId,
							checkName: _this.currentUser.name,
							checkStaff: '[' + _this.currentUser.userId + ']' + _this.currentUser.name,
							checkDate: this.getTime(new Date(), 'min'),
							biztypeId: 'RSP'
						};
						_this.photFarmForm.list = data.photFarmList.map((item, index) => {
							item.checkInfoId = mui.neiosCommon.getUuidv4(),
								item.checkId = _this.baseForm.checkId,
								item.biztypeId = 'RSP',
								item.sort = index + 1;
							return item;
						});
					} else {
						_this.baseForm = data.baseInfo;
						_this.photFarmForm.list = data.photFarmList.map((item, index) => {
							item.sort = index + 1;
							return item;
						});
					}
					_this.detailList = data.detailList;
				},
				(data) => {
					mui.toast("获取数据错误：" + data.msg);
				}
			);
		},
		clickStop() {

		},
		// 删除
		del(item, index) {
			var btnArray = ['取消', '确定'];
			mui.confirm('是否确认删除？', '提示', btnArray, (e) => {
				if (e.index == 1) {
					this.list.splice(index, 1)
					if (item.networkSafeInfoId != null && typeof(item.networkSafeInfoId) !=
						"undefined" && item.networkSafeInfoId !=
						"") {
						this.delNetSafeDeviceInfoList.push(item);
					}
				} else {

				}
			})

		},
		// 新增添加
		addList() {
			if (this.isEdit) {
				var arr= []
				arr = arr.concat(this.checkForm.nbList, this.checkForm.xbList, this.checkForm.hlList);
			this.detailList.forEach((item,index)=>{
				arr.forEach((item1,index1)=>{
					if(item.checkDetailId == item1.checkDetailId){
						this.detailList[index] = JSON.parse(JSON.stringify(item1))
						
					}
				})
			})
			
			} else {
				this.photFarmForm.list.push({
					checkInfoId: this.myUid,
					checkId: this.baseForm.checkId,
					biztypeId: 'RSP',
					stationName: '',
					sort: this.photFarmForm.list.length + 1
				});
				this.detailList = this.detailList.concat(this.checkForm.nbList, this.checkForm.xbList, this.checkForm.hlList);
			}
			this.addOrEdit = false
			
		},
		delPhotFarm: function(row) {
		
		            if(row.dataType == 'db') {
		
		                row.dataStatus = '0';
		
		                this.deleteList.push(row);
		
		            }
		
		            this.detailList.map(item => {
		
		                if(item.checkInfoId == row.checkInfoId) {
		
		                    item.dataStatus = '0';
		
		                }
		
		            });
		
		            let currIndex = -1;
		
		            this.photFarmForm.list = this.photFarmForm.list.filter((item, index) => {
		
		                if(item.checkInfoId == row.checkInfoId) {
		
		                    currIndex = index;
		
		                }
		
		                return item.checkInfoId != row.checkInfoId;
		
		            }).map((item, index) => {
		
		                item.sort = index + 1;
		
		                return item;
		
		            });
		
		            let lastIndex = this.photFarmForm.list.length - 1
		
		            currIndex = lastIndex < currIndex ? lastIndex : currIndex;
		
		            if(currIndex > -1) {
		
		                this.$refs.photFarmTable.setCurrentRow(this.photFarmForm.list[currIndex])
		
		            } else {
		
		                this.checkForm.nbList = []; // 逆变器
		
		                this.checkForm.xbList = []; // 箱变
		
		                this.checkForm.hlList = []; // 汇流箱
		
		            }
		
		        },
		// 清楚编辑信息
		clearFormateData() {
			this.addOrEdit = false
		
		},
		//新增
		add() {
			this.isEdit = false
			this.addOrEdit = true;
			this.checkForm = {
				nbList: [], // 逆变室
				xbList: [], // 箱变
				hlList: [] // 汇流箱
			},
			this.myUid = mui.neiosCommon.getUuidv4();
		 ['逆变器启动/停止', '逆变器温度', '逆变器状态', '逆变器输出电压', '逆变器输出电流'].map((item, index) => this.checkForm.nbList.push({                    checkDetailId:  mui.neiosCommon.getUuidv4(),                    checkInfoId: this.myUid,                    checkContent: item,                    result: '',                    sort: index + 1,                    checkType: '10'                }));                ['绕组温度', '负荷电流'].map((item, index) => this.checkForm.xbList.push({                    checkDetailId: mui.neiosCommon.getUuidv4(),                    checkInfoId: this.myUid,                    checkContent: item,                    result: '',                    sort: index + 1,                    checkType: '11'                }));                ['断路器闭/合', '异常报警', '通讯中断', '过温报警台数', '过电压报警', '需加强观察支路电流'].map((item, index) => this.checkForm.hlList.push({                    checkDetailId: mui.neiosCommon.getUuidv4(),                    checkInfoId: this.myUid,                    checkContent: item,                    result: '',                    sort: index + 1,                    checkType: '12'                }));
		},
		//编辑
		edit(row) {
			this.addOrEdit = true
			this.isEdit =true
			 this.checkForm = {
				 nbList:[],
				 xbList:[],
				 hlList:[]
			 }
			 
			  this.checkForm = JSON.parse(JSON.stringify(this.checkForm))
			this.checkForm.nbList = this.detailList.filter((item, index) => item.checkInfoId == row.checkInfoId && item.checkType == '10'); // 逆变器            this.checkForm.xbList = this.detailList.filter((item, index) => item.checkInfoId == row.checkInfoId && item.checkType == '11'); // 箱变            this.checkForm.hlList = this.detailList.filter((item, index) => item.checkInfoId == row.checkInfoId && item.checkType == '12'); // 汇流箱
					this.checkForm = JSON.parse(JSON.stringify(this.checkForm))            if(this.checkForm.nbList.length == 0 || this.checkForm.xbList.length == 0 || this.checkForm.hlList.length == 0) {                ['逆变器启动/停止', '逆变器温度', '逆变器状态', '逆变器输出电压', '逆变器输出电流'].map((item, index) => this.checkForm.nbList.push({                    checkDetailId: mui.neiosCommon.getUuidv4(),                    checkInfoId: row.checkInfoId,                    checkContent: item,                    result: '',                    sort: index + 1,                    checkType: '10'                }));                ['绕组温度', '负荷电流'].map((item, index) => this.checkForm.xbList.push({                    checkDetailId: mui.neiosCommon.getUuidv4(),                    checkInfoId: row.checkInfoId,                    checkContent: item,                    result: '',                    sort: index + 1,                    checkType: '11'                }));                ['断路器闭/合', '异常报警', '通讯中断', '过温报警台数', '过电压报警', '需加强观察支路电流'].map((item, index) => this.checkForm.hlList.push({                    checkDetailId: mui.neiosCommon.getUuidv4(),                    checkInfoId: row.checkInfoId,                    checkContent: item,                    result: '',                    sort: index + 1,                    checkType: '12'                }));                this.detailList = this.detailList.concat(this.checkForm.nbList, this.checkForm.xbList, this.checkForm.hlList);				this.detailList = JSON.parse(JSON.stringify( this.detailList))            }			          
		
		},
		//获取时间
		getTime(myDate, type) {
			console.log(type)
			var realYear = myDate.getFullYear();
			var myMonth = myDate.getMonth() + 1;
			var realMonth = myMonth;
			var myDay = myDate.getDate();
			var realDay = myDay;
			var myHour = myDate.getHours();
			var realHours = myHour;
			var myMinute = myDate.getMinutes();
			var realMinute = myMinute;
			var mySecond = myDate.getSeconds();
			var realSecond = mySecond;
			if (myMonth < 10) {
				realMonth = "0" + myMonth;
			}
			if (myDay < 10) {
				realDay = "0" + myDay;
			}
			if (myMinute < 10) {
				realMinute = "0" + myMinute;
			}
			if (myHour < 10) {
				realHours = "0" + myHour;
			}
			if (mySecond < 10) {
				realSecond = "0" + mySecond;
			}
			var realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours + ':' + realMinute +
				':' +
				realSecond;
			if (type == "sec") {
				realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours + ':' + realMinute +
					':' + realSecond;
			} else if (type == "min") {
				realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours + ':' + realMinute;
			} else if (type == "hour") {
				realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours;
			} else if (type == "day") {
				realDate = realYear + '-' + realMonth + '-' + realDay;
			} else if (type == "month") {
				realDate = realYear + '-' + realMonth;
			} else if (type == "year") {
				realDate = realYear;
			}
			return realDate;
		},
		navTo(e, data) {
			if (data) {
				window.localStorage.setItem("itme", JSON.stringify(data))
			} else {
				window.localStorage.setItem("itme", '')
			}
			mui.openWindow({
				url: e,
				id: 'my',
				extras: {
					'item': data
				},
				waiting: {
					autoShow: false
				}
			});
		},
		// 清除隐患描述
		clearDefectInfo() {
			this.showAdd = false
			this.isApprove = false
			this.defectInfoList = []
			this.approveDesc = ''
		},
		// 初始化隐患描述
		initDefectInfo() {
			this.showAdd = true
			this.list.forEach(e => {
				if (e.isWarning != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有告警。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isAmbientNormal != '1') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：运行环境是否正常。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isHaveAbnormalDevice != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有非正常业务设备。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
			})
		},
		// 初始化通过隐患描述
		approveInitDefectInfo(type) {
			if (type == 'approve') {
				this.approveDialogTips = true
			} else {
				this.approveDialogTips = false
			}
			this.isApprove = true
			this.list.forEach(e => {
				if (e.isWarning != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有告警。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isAmbientNormal != '1') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：运行环境是否正常。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isHaveAbnormalDevice != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有非正常业务设备。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
			})
		},
		getName(data) {
			console.log(data)
			if (data) {
				let list = []
				data.split(',').map((v) => {
					list.push(v.split(']')[1])
				})
				return list.join(',');
			} else {
				return data
			}

		},
		// 保存
		save() {
			let _this = this;
			if (this.photFarmForm.list.length == 0) {
				mui.toast("请至少添加一个风场");
				return;
			}
			let param = {
				currentUser:this.currentUser,
				baseInfo: this.baseForm, // 基本信息
				photFarmList: this.photFarmForm.list, // 风电场列表
				deleteList: this.deleteList, // 删除的风电场列表
				detailList: this.detailList // 巡检信息列表
			};
			console.log(JSON.stringify(param))
			// return
			mui.neiosRequst.httpRequst('/app/photFarmCheck/savePhotFarmCheckData', param,
				(data) => {
					// this.goBack()
					mui.toast("保存成功");
					setTimeout(function() {
						location.reload();
					}, 1000);
				
				},
				(data) => {
					mui.toast("保存错误：" + data.msg);
				}
			);
		},
		// 提交
		submit() {
			let _this = this;
			if (this.photFarmForm.list.length == 0) {
				mui.toast("请至少添加一个电场");
				return;
			}
			let check = true;
			check = check && !this.photFarmForm.list.some((item, index) => {
				let checkList = this.detailList.filter(it => it.checkInfoId == item.checkInfoId);
				let checkFailed = checkList.length == 0 || checkList.some(it => it.result == '');
				if (checkFailed) {
					// this.$refs.windFarmTable.setCurrentRow(this.windFarmForm.list[index]);
					mui.toast(`请输入场站：${this.photFarmForm.list[index].stationName}内容`);
				}
				return checkFailed;
			});
			this.photFarmForm.list.map((v, i) => {
				if (!v.stationName) {
					mui.toast("请填写场站名称");
					check = false
					return
				}
			})
			if (check) {
				let param = {
					currentUser:this.currentUser, 	
					baseInfo: this.baseForm, // 基本信息
					photFarmList: this.photFarmForm.list, // 风电场列表
					deleteList: this.deleteList, // 删除的风电场列表
					detailList: this.detailList // 巡检信息列表
				};
				console.log(JSON.stringify(param))
				
				mui.neiosRequst.httpRequst('/app/photFarmCheck/submitPhotFarmCheckData', param,
					(data) => {
						
						mui.toast("保存成功");
						setTimeout(function() {
							_this.goBack()
						}, 1000);
					},
					(data) => {
						mui.toast("保存错误：" + data.msg);
					}
				);
			}
		},
		//返回上一页
		goBack() {
			let page2 = plus.webview.getWebviewById("../sitOperation/terminalwork/terminalwork.html");
			let param = {}
			param.isRefresh = true
			mui.fire(page2, 'isRefresh', param);
			mui.back()
		},
	
		// 光伏电场列表当前选中行改变时

		currentChange: function(row, oldRow) {

			this.checkForm.nbList = this.detailList.filter((item, index) => item.checkInfoId == row
				.checkInfoId && item.checkType == '10'); // 逆变器

			this.checkForm.xbList = this.detailList.filter((item, index) => item.checkInfoId == row
				.checkInfoId && item.checkType == '11'); // 箱变

			this.checkForm.hlList = this.detailList.filter((item, index) => item.checkInfoId == row
				.checkInfoId && item.checkType == '12'); // 汇流箱

			if (this.checkForm.nbList.length == 0 || this.checkForm.xbList.length == 0 || this.checkForm
				.hlList.length == 0) {

				['逆变器启动/停止', '逆变器温度', '逆变器状态', '逆变器输出电压', '逆变器输出电流'].map((item, index) => this.checkForm
					.nbList.push({

						checkDetailId: mui.neiosCommon.getUuidv4(),

						checkInfoId: row.checkInfoId,

						checkContent: item,

						result: '',

						sort: index + 1,

						checkType: '10'

					}));

				['绕组温度', '负荷电流'].map((item, index) => this.checkForm.xbList.push({

					checkDetailId: mui.neiosCommon.getUuidv4(),

					checkInfoId: row.checkInfoId,

					checkContent: item,

					result: '',

					sort: index + 1,

					checkType: '11'

				}));

				['断路器闭/合', '异常报警', '通讯中断', '过温报警台数', '过电压报警', '需加强观察支路电流'].map((item, index) => this
					.checkForm.hlList.push({

						checkDetailId: mui.neiosCommon.getUuidv4(),

						checkInfoId: row.checkInfoId,

						checkContent: item,

						result: '',

						sort: index + 1,

						checkType: '12'

					}));

				this.detailList = this.detailList.concat(this.checkForm.nbList, this.checkForm.xbList, this
					.checkForm.hlList);

			}

			this.checkForm.nbList.every(item => item.result == '') && this.checkForm.xbList.every(item =>
					item.result == '')

				&&
				this.checkForm.hlList.every(item => item.result == '')

				?
				this.$refs.checkForm.map(item => item.clearValidate()) : this.$refs.checkForm.map(item =>
					item.validate());

		},



	}

})
